<template>
	<s-layout title="货源推荐" navbar="normal1">
		<!-- 导航栏 -->
    <assNavbar :title="enumType[state.type]"></assNavbar>
		<!-- 内容 -->
		<view class="distribution-content-box">
			<view class="distribution-content">
				<ProductCard class="product-card"
					v-for="(product, index) in state.products" 
					:key="index"
					:product="product"
					@action="handleProductAction"
				/>
			</view>
		</view>

		<uni-load-more
      v-if="state.pagination.total > 0"
      :status="state.loadStatus"
      :content-text="{
        contentdown: '上拉加载更多',
      }"
      @tap="loadmore"
    />
    <s-empty v-if="state.pagination.total === 0" icon="/static/soldout-empty.png" text="暂无数据"></s-empty>

	</s-layout>
</template>

<script setup>
	import { ref, reactive, computed } from 'vue'
	import {
		onLoad,
		onShow,
		onHide,
		onPageScroll,
		onReachBottom,
		onPullDownRefresh
	} from '@dcloudio/uni-app';
	
	import sheep from '@/sheep';
	import assNavbar from '@/components/ass-navbar.vue';
	import ProductCard from './components/ProductCard.vue';

	const statusBarHeight = sheep.$platform.device.statusBarHeight; // 状态栏高度

	// 枚举
	const enumType = {
		quick: '快速出单好货',
		hot: '站外爆款热销',
		supplier: '严选供货商',
	}

	const state = reactive({
		type: 'quick', // 类型名称
		pagination: {
      current_page: 1,
      last_page: 1,
      total: 0,
    },
    loadStatus: '', // 加载更多状态 ['more' | 'noMore']

		products: [
			// {
			// 	title: '暖宝宝暖贴袋鼠保暖贴暖服帖自发热鞋垫暖足贴贴暖宝宝暖贴袋鼠保暖贴暖服帖自发热鞋垫暖足贴贴',
			// 	price: '￥0.20-￥3.40',
			// 	sales: '1万+',
			// 	isAdded: false
			// }
		],
	})

	let listParams = reactive({
    page: 1,
    list_rows: 10, // 每页20条数据
  })

	const userInfo = computed(() => sheep.$store('user').userInfo);

	const resetData = () => {
    state.products = []
    state.pagination = {
      current_page: 1,
      last_page: 1,
      total: 0
    }
    state.loadStatus = ''
  }

	const handleProductAction =async  (product) => {
		const res = await sheep.$api.distribution.addGoods(userInfo.value.cyq_store_id,product.id)
		if(res.error == 0){
			show.value = true
		} else {
			console.log('msg',res)
			uni.showToast({
				icon:'none',
				title:msg
			})
		}
	}
	const getListData = async () => {
		try {
			let listData = null
			// supplier=严选 hot=爆款 quick=快速出单
			if (state.type == 'supplier') {
				listData = await sheep.$api.distribution.shopSelectList({ // 3325=严选 3324=爆款 3323=出单好货
					...listParams,
					seacrch: JSON.stringify({ "category_ids":[3325,"="] })
				})
			} else if (state.type == 'hot') {
				listData = await sheep.$api.distribution.shopSelectList({
					...listParams,
					seacrch: JSON.stringify({ "category_ids":[3324,"="] })
				})
			} else if (state.type == 'quick') {
				listData = await sheep.$api.distribution.shopSelectList({
					...listParams,
					seacrch: JSON.stringify({ "category_ids":[3323,"="] })
				})
			}
			
			console.log(listData, 'listData')

			if (listData.error == 0) {
				const { total, current_page, last_page } = listData.data
				listParams.page = current_page
				if (current_page === 1) {
					state.products = listData.data.data;
				} else {
					// 如果是加载更多，则追加数据
					state.products = [...state.products, ...listData.data.data]
				}
        

        Object.assign(state.pagination, { total, current_page, last_page })

        if (state.pagination.current_page < state.pagination.last_page) {
          state.loadStatus = 'more';
        } else {
          state.loadStatus = 'noMore';
        }

        return listData.data.data
      }
		} catch (err) {
			console.log(err)
		}
	}

	const loadmore = () => {
    if (state.loadStatus !== 'noMore') {
      state.pagination.current_page ++ 
      listParams.page ++ 
      getListData();
    }
  }

	onReachBottom(() => {
    console.log('触底了。')
     loadmore()
  });

	onLoad((options) => {
		console.log('onLoad', options)
		state.type = options.type
		// supplier=严选 hot=爆款 quick=快速出单
		loadmore()
	})
</script>

<style lang="scss" scoped>
@import './style/list.scss';
</style>

